﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.Orders.IViews;
using Innova.FoundationFramework.DataTypes;
using MyCommerce.Business.Stores;

namespace MyCommerce.Presentation.Orders.Presenters
{
    public class OrdersToSuppliersAdministrationPresenter
    {
        private IOrdersToSuppliersAdministrationView pViewToPresent;

        public OrdersToSuppliersAdministrationPresenter(IOrdersToSuppliersAdministrationView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancelOrder += new EventHandler(pViewToPresent_OnCancelOrder);
            pViewToPresent.OnLoadOrders += new EventHandler(pViewToPresent_OnLoadOrders);
            pViewToPresent.OnLoadView += new Innova.FoundationFramework.DataTypes.GenericEventHandler<long>(pViewToPresent_OnLoadView);
            pViewToPresent.OnNewOrder += new EventHandler(pViewToPresent_OnNewOrder);
            pViewToPresent.OnViewDetail += new EventHandler(pViewToPresent_OnViewDetail);
            pViewToPresent.OnDeliveryOrder += new EventHandler(pViewToPresent_OnDeliveryOrder);
        }

        private void pViewToPresent_OnDeliveryOrder(object sender, EventArgs e)
        {
            if (pViewToPresent.InvokeViewMode(pViewToPresent.ObjOrderSelect.IdOrderSupplier, false))
                LoadOrdersInView();
        }

        private void pViewToPresent_OnViewDetail(object sender, EventArgs e)
        {
            pViewToPresent.InvokeViewMode(pViewToPresent.ObjOrderSelect.IdOrderSupplier, true);
        }

        private void pViewToPresent_OnNewOrder(object sender, EventArgs e)
        {
            if (pViewToPresent.InvokeNewOrderForm())
                LoadOrdersInView();
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<long> e)
        {
            StoreManager storeMgr = new StoreManager();
            pViewToPresent.ColStoresLoad = storeMgr.GetStoresActives();

            pViewToPresent.IdStoreSelect = e.EventData;

            LoadOrdersInView();
        }               

        private void pViewToPresent_OnLoadOrders(object sender, EventArgs e)
        {
            LoadOrdersInView();
        }

        private void pViewToPresent_OnCancelOrder(object sender, EventArgs e)
        {
            OrderSupplierManager orderMgr = new OrderSupplierManager();
            orderMgr.CancelOrderSupplier(pViewToPresent.ObjOrderSelect.IdOrderSupplier);
            LoadOrdersInView();
        }
        
        private void LoadOrdersInView()
        {
            if (pViewToPresent.IdStoreSelect != 0)
            {
                OrderSupplierManager orderMgr = new OrderSupplierManager();
                pViewToPresent.ColOrdersLoad = orderMgr.GetOrdersByIdStoreAndUser(pViewToPresent.IdStoreSelect, pViewToPresent.UserId);
            }
        }
    }
}
